import { useUserStore } from "@/stores/user";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import type {
  chapterForm,
  chapterRequest,
  courseForm,
  courseStudent,
} from "./type";

enum API {
  CREATE_COURSE = "/course/creatCourse",
  COURSE_LIST = "/course/courseList",
  GET_STUDENT = "/detail",
  REMOVE = "/course/delete",
}
export const createCourse = (course: any) => {
  const param = new URLSearchParams();
  for (let key in course) {
    param.append(key, course[key]);
  }
  return request.post(API.CREATE_COURSE, param);
};

// 获取课程的接口方法
export const getCourseList = () => {
  const useStore = useUserStore();
  if (useStore.userInfo && useStore.userInfo.id) {
    let userid = useStore.userInfo.id;
    return request.get(`${API.COURSE_LIST}/${userid}`);
  } else {
    ElMessage.error("用户信息或用户ID不存在");
    // 可以根据实际情况返回相应的处理逻辑
  }
};
export const deleteCourseByCid = (cid: any) => {
  return request.delete(`${API.REMOVE}/${cid}`);
};

export const updateCourse = (course: courseForm) => {
  return request.put("/course/updateCourse", course);
};
// 根据具体的课程获取学生信息
export const getStudent = (cid: any) => {
  return request.get(`${API.GET_STUDENT}/${cid}`);
};
//创建新章节
// export const createChapter = (chapter: any) => {
//   return request.post("/detail/createChapter", chapter);
// };
export const createChapter1 = (chapter: chapterRequest) => {
  return request.post("/detail/createChapter1", chapter);
};
export const createChapter2 = (questionsIds: any) => {
  return request.post("/detail/createChapter2", questionsIds);
};
//获取章节
export const getChapter = (cid: any) => {
  return request.get(`${"/detail/chapter"}/${cid}`);
};
export const updateChapter = (newChapter: chapterForm) => {
  return request.put("/detail/chapter", newChapter);
};
//学生端
export const getStudentCourse = (student_id: number) => {
  return request.get(`${"/course/studentCourse"}/${student_id}`);
};
export const getChapterQuestions = (chapterId: number) => {
  return request.get(`${"/detail/chapterQuestions"}/${chapterId}`);
};
export const getChapterPoint = (chapterId: number) => {
  return request.get(`${"/detail/chapterPoint"}/${chapterId}`);
};
export const getChapterDetail = (id: number) => {
  return request.get(`${"/detail/chapterById"}/${id}`);
};
export const addStudentsToCourse = (courseStudent: courseStudent) => {
  return request.post("/course/addStudentsToCourse", courseStudent);
};
export interface courseFileForm {
  id?: number;
  title?: string;
  content?: string;
  file?: string;
  cid?: number;
}

export const getCourseFile = (cid: number) => {
  return request.get(`${"/detail/courseFile"}/${cid}`);
};

export interface commentForm {
  id?: number;
  studentUsername?: string;
  content?: string;
  createTime?: string;
  courseId?: number;
  avatar?: string;
}
export const getComment = (cid: number) => {
  return request.get(`${"/detail/comment"}/${cid}`);
};
export const addComment = (comment: commentForm) => {
  return request.post("/detail/addComment", comment);
};
